package com.test.springboot.springBootUtils.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeansException;
import org.springframework.core.env.Environment;
import org.springframework.web.filter.GenericFilterBean;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LogCostFilter extends GenericFilterBean {

	@Override
	public void setBeanName(String beanName) {
		log.info("beanName:{}", beanName);
		super.setBeanName(beanName);
		
	}

	@Override
	public void setEnvironment(Environment environment) {
		super.setEnvironment(environment);
	}

	@Override
	public Environment getEnvironment() {
		return super.getEnvironment();
	}

	@Override
	protected Environment createEnvironment() {
		return super.createEnvironment();
	}

	@Override
	public void setServletContext(ServletContext servletContext) {
		super.setServletContext(servletContext);
	}

	@Override
	public void afterPropertiesSet() throws ServletException {
		super.afterPropertiesSet();
	}

	@Override
	public void destroy() {
		super.destroy();
	}

	@Override
	protected void initBeanWrapper(BeanWrapper bw) throws BeansException {
		super.initBeanWrapper(bw);
	}

	@Override
	protected void initFilterBean() throws ServletException {
		super.initFilterBean();
	}

	@Override
	public FilterConfig getFilterConfig() {
		return super.getFilterConfig();
	}

	@Override
	protected String getFilterName() {
		return super.getFilterName();
	}

	@Override
	protected ServletContext getServletContext() {
		return super.getServletContext();
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		long start = System.currentTimeMillis();
		// 放过当前的 方法
		chain.doFilter(request, response);
		// 拦截回来的方法
		log.info("Execute cost={}" , (System.currentTimeMillis() - start));

	}

}
